//
//  MyAVAudioPlayer.m
//  哈哈
//
//  Created by 奇艺果 on 2017/12/13.
//  Copyright © 2017年 奇艺果. All rights reserved.
//

#import "MyAVAudioPlayer.h"

@implementation MyAVAudioPlayer
static NSMutableDictionary *_players;

-(instancetype)init{
    if (self == [super init]) {
        
    }
    return self;
}
#pragma mark 单例模式(避免同时播放多首歌)
+(instancetype)sharedAVAudioPlayer{
    static MyAVAudioPlayer *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}
#pragma mark 通过音乐名称播放音乐
-(void)playMusicWithMusicName:(NSString *)musicName{
    
    // 2.1.获取对应音乐资源
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:musicName withExtension:@"mp3"];
    if (fileUrl == nil) return;
    // 2.2.创建对应的播放器
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
    // 2.4.准备播放
    [_player prepareToPlay];
    [_player setNumberOfLoops:1000000];
    // 3.播放音乐
    [_player play];
    
}
-(void)playOrStopMusic{
    if ([_player isPlaying]) {
        [_player pause];
        return;
    }
    [_player play];
}

- (void)stopMusic{
    
    if ([_player isPlaying]) {
        [_player pause];
        _player = nil;
        return;
    }
}
@end
